package com.opages.netty.demo.lesson.dark_horse2;

import com.opages.netty.demo.lesson.ByteBufferUtil;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @author daizhong.liu
 * @create 2021-12-29 17:21
 **/
@Slf4j
public class Client {
    //模拟数据太大buffer装不下分几次装载
    public static void main(String[] args) {
        try (SocketChannel socketChannel = SocketChannel.open()) {
            socketChannel.connect(new InetSocketAddress("127.0.0.1",8889));
            socketChannel.configureBlocking(false);
            Selector selector = Selector.open();
            ByteBuffer byteBuffer = ByteBuffer.allocate(32);
            socketChannel.register(selector, SelectionKey.OP_READ,byteBuffer);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (true) {
                            int select = selector.select();
                            if(select>0) {
                                log.debug("存在事件...");
                                Iterator<SelectionKey> iteratorKeys = selector.selectedKeys().iterator();
                                while (iteratorKeys.hasNext()) {
                                    SelectionKey key = iteratorKeys.next();
                                    if (key.isReadable()) {
                                        log.debug("客户端读事件");
                                        ByteBuffer attachment = (ByteBuffer) key.attachment();
                                        int read = socketChannel.read(attachment);
//                                        SocketChannel channel = (SocketChannel) key.channel();
//                                        ByteBuffer attachment = ByteBuffer.allocate(32);
//                                        int read = channel.read(attachment);
//                                        if(read > 0) {
//                                            log.debug("读取内容");
//                                            ByteBufferUtil.debugAll(attachment);
//                                        }
                                        if (read == -1) {
                                            log.debug("读取完成..");
                                            ByteBufferUtil.debugAll(attachment);
                                        } else {
                                            if (attachment.position() == attachment.limit()) {
                                                ByteBuffer newBuffer = ByteBuffer.allocate(attachment.capacity() * 2);
                                                // 将旧buffer中的内容放入新的buffer中
                                                attachment.flip();
                                                newBuffer.put(attachment);
                                                // 将新buffer放到key中作为附件
                                                key.attach(newBuffer);
                                            }else {
                                                log.debug("读取内容");
                                                ByteBufferUtil.debugAll(attachment);
                                            }
                                        }
                                    }
                                    iteratorKeys.remove();
                                }
                            }
                            Thread.sleep(3000);
                        }
                    }catch(Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            //写数据
            log.debug("等待输入:");
            Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextLine()) {
                String content = scanner.nextLine();
                socketChannel.write(ByteBuffer.wrap(content.getBytes(CharsetUtil.UTF_8)));
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
